/*
 * Created by JFormDesigner on Thu Jun 23 17:41:41 CST 2022
 */

package jw3.c10.view;

import jw3.c10.controllner.ScenicspotController;
import jw3.c10.controllner.StationController;
import jw3.c10.model.Scenicspot;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class SPlogin extends JFrame {
    private static List<Scenicspot> scenicspotList;
    private static int id;//景点编号
    public SPlogin() {
        initComponents();
        this.setSize(1600,1200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        label3.setFont(new Font("宋体",Font.PLAIN,30));
    }
    private void showData() {
        scenicspotList= ScenicspotController.getAll();

        int row=(int) Math.ceil(scenicspotList.size()/4.0);

        JPanel panel2=new JPanel();//每次循环都重新生成
        panel2.setLayout(new GridLayout(row,4));

        //循环生成，动态绑定
        for (int i = 0; i < scenicspotList.size(); i++) {
            JPanel panel=new JPanel();
            Scenicspot s=scenicspotList.get(i);
            JLabel jLabel1=new JLabel();
            ImageIcon imageIcon=
                    new ImageIcon("images/"+s.getImg());
            imageIcon.setImage(imageIcon.getImage().
                    getScaledInstance(210,100,1));//调大小
            jLabel1.setIcon(imageIcon);
            JLabel jLabel2=new JLabel(s.getScenicspotName());
            JLabel jLabel3=new JLabel("人流量："+s.getNum()+"人");
            JButton btn=new JButton("去这里");

            //添加监听器
            btn.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    label1.setIcon(imageIcon);
                    int r=s.getSID();
                    String a= StationController.getStation(r);
                    label2.setText("每日人流量为"+s.getNum()+"此地在"+a+"附近");
                    dialog1.setVisible(true);
                }
            });

            jLabel1.setBounds(10,10,100,100);
            jLabel2.setBounds(10,115,60,50);
            jLabel3.setBounds(80,115,60,50);
            btn.setBounds(30,170,80,40);

            panel.setLayout(null);
            panel.setSize(210,340);
            panel.add(jLabel1);
            panel.add(jLabel2);
            panel.add(jLabel3);
            panel.add(btn);

            panel2.add(panel);

        }
        int h=row*230;
        panel2.setPreferredSize(new Dimension(1000,h));

        scrollPane1.setViewportView(panel2);
    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
        this.dispose();
        new Rlogin();
    }

    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
        dialog1.dispose();
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
        dialog1.dispose();
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        panel2 = new JPanel();
        button3 = new JButton();
        label3 = new JLabel();
        dialog1 = new JDialog();
        label1 = new JLabel();
        label2 = new JLabel();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //======== scrollPane1 ========
            {

                //======== panel2 ========
                {
                    panel2.setLayout(new GridLayout(1, 4));
                }
                scrollPane1.setViewportView(panel2);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(0, 0, 1100, 900);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 80, 1100, 900);

        //---- button3 ----
        button3.setText("\u8fd4\u56de");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3);
        button3.setBounds(540, 30, 78, 30);

        //---- label3 ----
        label3.setText("\u666f\u70b9\u63a8\u8350");
        contentPane.add(label3);
        label3.setBounds(350, 0, 195, 60);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(null);
            dialog1ContentPane.add(label1);
            label1.setBounds(90, 30, 150, 155);

            //---- label2 ----
            label2.setText("text");
            dialog1ContentPane.add(label2);
            label2.setBounds(40, 210, 270, 90);

            //---- button1 ----
            button1.setText("\u786e\u5b9a");
            button1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button1MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button1);
            button1.setBounds(new Rectangle(new Point(55, 320), button1.getPreferredSize()));

            //---- button2 ----
            button2.setText("\u8fd4\u56de");
            button2.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button2MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button2);
            button2.setBounds(new Rectangle(new Point(210, 320), button2.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog1ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog1ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog1ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog1ContentPane.setMinimumSize(preferredSize);
                dialog1ContentPane.setPreferredSize(preferredSize);
            }
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        showData();
    }



    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JPanel panel2;
    private JButton button3;
    private JLabel label3;
    private JDialog dialog1;
    private JLabel label1;
    private JLabel label2;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
